/*
' Copyright (c) 2011 DotNetNuke Corporation
'  All rights reserved.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'
*/

using System;
using System.Web.UI.HtmlControls;
using DotNetNuke.Framework;

namespace DotNetNuke.Modules._2DoTasks
{
    public class _2DoTasksModuleBase : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        #region "Constants"

        #region "Css"

        private const string CSS_2DoTasksJqueryUiId = "_2DoTasksJqueryUiId";
        private const string CSS_2DoTasksJqueryUiPath = "/Css/jquery-ui.css";

        #endregion "Css"

        #region "Js"

        private const string JS_2DoTasksAdditionalMethodsId = "_2DoTasksAdditionalMethods";
        private const string JS_2DoTasksAdditionalMethodsPath = "/Js/additional-methods.js";
        private const string JS_2DoTasksJqueryValidateMinId = "_2DoTasksJqueryValidateMin";
        private const string JS_2DoTasksJqueryValidateMinPath = "/Js/jquery.validate.min.js";

        private const string JS_2DoTasksUtilsId = "_2DoTasksUtils";
        private const string JS_2DoTasksUtilsPath = "/Js/_2DoTasksUtils.js";
        private const string JS_2DoTasksJqueryMetadataId = "_2DoTasksJqueryMetadata";
        private const string JS_2DoTasksJqueryMetadataPath = "/Js/jquery.metadata.js";

        #endregion "Js"

        #endregion "Constants"

        //with this base class you can provide any custom properties and methods that all your controls can access here, you can also access all the DNN
        // methods and properties available off of portalmodulebase such as TabId, UserId, UserInfo, etc.

        #region "Properties"

        #region "Task Properties"

        /// <summary>
        /// Evaluates if a simple task was created
        /// </summary>
        public bool SimpleTask_Created
        {
            get
            {
                var qs = Request.QueryString["sTsk"];
                if (qs != null)
                {
                    int val;
                    return int.TryParse(qs, out val) && val == 1;
                }
                return false;
            }
        }

        /// <summary>
        /// Evaluates if a task was created
        /// </summary>
        public bool Task_Created
        {
            get
            {
                var qs = Request.QueryString["Tsk"];
                if (qs != null)
                {
                    int val;
                    return int.TryParse(qs, out val) && val == 1;
                }
                return false;
            }
        }

        /// <summary>
        /// Evaluates if a task was updated
        /// </summary>
        public bool Task_Updated
        {
            get
            {
                var qs = Request.QueryString["Tsk"];
                if (qs != null)
                {
                    int val;
                    return int.TryParse(qs, out val) && val == 2;
                }
                return false;
            }
        }

        /// <summary>
        /// Evaluates if a task was deleted
        /// </summary>
        public bool Task_Deleted
        {
            get
            {
                var qs = Request.QueryString["Tsk"];
                if (qs != null)
                {
                    int val;
                    return int.TryParse(qs, out val) && val == 3;
                }
                return false;
            }
        }

        /// <summary>
        /// Evaluates if task id is passed
        /// </summary>
        public Nullable<int> TaskId
        {
            get
            {
                var qs = Request.QueryString["tid"];
                if (qs != null)
                    return Convert.ToInt32(qs);
                return null;
            }
        }

        #endregion "Task Properties"

        #region "Project Properties"

        /// <summary>
        /// Evaluates if a Project was created
        /// </summary>
        public bool Project_Created
        {
            get
            {
                var qs = Request.QueryString["Prj"];
                if (qs != null)
                {
                    int val;
                    return int.TryParse(qs, out val) && val == 1;
                }
                return false;
            }
        }

        /// <summary>
        /// Evaluates if a Project was updated
        /// </summary>
        public bool Project_Updated
        {
            get
            {
                var qs = Request.QueryString["Prj"];
                if (qs != null)
                {
                    int val;
                    return int.TryParse(qs, out val) && val == 2;
                }
                return false;
            }
        }

        /// <summary>
        /// Evaluates if a Project was deleted
        /// </summary>
        public bool Project_Deleted
        {
            get
            {
                var qs = Request.QueryString["Prj"];
                if (qs != null)
                {
                    int val;
                    return int.TryParse(qs, out val) && val == 3;
                }
                return false;
            }
        }

        /// <summary>
        /// Evaluates if Project id is passed
        /// </summary>
        public Nullable<int> ProjectId
        {
            get
            {
                var qs = Request.QueryString["pid"];
                if (qs != null)
                    return Convert.ToInt32(qs);
                return null;
            }
        }

        #endregion "Project Properties"

        #endregion "Properties"

        #region "Events"

        protected void Page_Init(object sender, System.EventArgs e)
        {
            if (!jQuery.AreDnnPluginsRequested)
                jQuery.RequestDnnPluginsRegistration();

            //Include Css files
            AddStylePageHeader(CSS_2DoTasksJqueryUiId, CSS_2DoTasksJqueryUiPath);

            //Include js files
            AddJavascriptPageHeader(JS_2DoTasksJqueryValidateMinId, JS_2DoTasksJqueryValidateMinPath);
            AddJavascriptPageHeader(JS_2DoTasksAdditionalMethodsId, JS_2DoTasksAdditionalMethodsPath);
            AddJavascriptPageHeader(JS_2DoTasksJqueryMetadataId, JS_2DoTasksJqueryMetadataPath);
            AddJavascriptPageHeader(JS_2DoTasksUtilsId, JS_2DoTasksUtilsPath);
        }

        #endregion "Events"

        #region "Aux Functions"

        /// <summary>
        /// Adds a page header of type javascript
        /// </summary>
        /// <param name="jsId">the id of the header tag</param>
        /// <param name="srcPath">the javascript path to the source file</param>
        private void AddJavascriptPageHeader(string jsId, string srcPath)
        {
            HtmlGenericControl scriptInclude = (HtmlGenericControl)Page.Header.FindControl(jsId);
            if (scriptInclude == null)
            {
                scriptInclude = new HtmlGenericControl("script");
                scriptInclude.Attributes["type"] = "text/javascript";
                scriptInclude.Attributes["src"] = this.TemplateSourceDirectory + srcPath;
                scriptInclude.ID = jsId;

                Page.Header.Controls.Add(scriptInclude);
            }
        }

        /// <summary>
        /// Adds a page header of type css
        /// </summary>
        /// <param name="cssId">the id of the header tag</param>
        /// <param name="cssPath">the css path to the source file</param>
        private void AddStylePageHeader(string cssId, string cssPath)
        {
            HtmlGenericControl scriptInclude = (HtmlGenericControl)Page.Header.FindControl(cssId);
            if (scriptInclude == null)
            {
                scriptInclude = new HtmlGenericControl("link");
                scriptInclude.Attributes["rel"] = "stylesheet";
                scriptInclude.Attributes["type"] = "text/css";
                scriptInclude.Attributes["href"] = this.TemplateSourceDirectory + cssPath;
                scriptInclude.ID = cssId;

                Page.Header.Controls.Add(scriptInclude);
            }
        }

        #endregion "Aux Functions"
    }
}